var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifLen: false,
    buthiddenNext: false,
    buthiddenSubmit: false,
    nextShow: true,
    telephone: '',
    code: '',
    verifyCodeTime: '获取验证码',
    timer: '',
    loginType: '1',
    password: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
  },
  chooseType(e) {
    // console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == this.data.loginType) {
      return;
    }
    this.setData({
      loginType: e.currentTarget.dataset.type,
      code: '',
      password: ''
    });
  },
  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ telephone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ telephone: e.detail.value.replace(/\D/g, '') });
    }
    if (e.detail.value.length == 11) {
      wx.request({
        method: 'POST',
        url: app.preUrl + 'queryTelephone',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: that.data.telephone
        },
        success: function(res) {
          console.log(res);
          if (res.data.code == 205) {
            that.setData({ ifLen: true });
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: 'registerNext'
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });

            that.setData({ ifLen: false });
          }
        },
        fail: function(error) {}
      });
    } else {
      that.setData({ ifLen: false });
    }
  },
  //手机号验证码前端校验
  onCodeEvent: function(e) {
    var that = this;
    that.setData({ code: e.detail.value.replace(/\D/g, '') });
  },
  onPassword(e) {
    var that = this;
    that.setData({ password: e.detail.value.replace(/(^\s*)|(\s*$)/g, '') });
  },
  forgetPassword() {
    wx.navigateTo({
      url: 'modifyPassword'
    });
  },
  //提交手机号获取验证码
  onPhoneNext: function() {
    var that = this;
    var telephone = that.data.telephone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var c = 60;
    if (!that.data.ifLen) {
      return;
    }
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      });
      return false;
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else {
      that.setData({ buthiddenNext: true });
      that.data.timer = setInterval(function() {
        c = c - 1;
        that.setData({
          verifyCodeTime: c + 's后重发'
        });
        if (c == 0) {
          clearInterval(that.data.timer);
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          });
        }
      }, 1000);

      wx.request({
        method: 'POST',
        url: app.preUrl + 'obtainMessageLogin',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          telephone: that.data.telephone
        },
        success: function(res) {
          if (res.data.code == 100) {
            that.setData({ buthiddenNext: true });
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            clearInterval(that.data.timer);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            });
          }
        },
        fail: function(error) {
          that.setData({ buthiddenNext: false });
        }
      });
    }
  },

  onPhoneSubmit: function(e) {
    var that = this;
    var sendData = {};
    var telephone = that.data.telephone;
    var code = that.data.code;
    var password = that.data.password;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (telephone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      });
      return false;
    }
    if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none'
      });
      return false;
    }
    if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none'
      });
      return false;
    }
    if (that.data.loginType == '1') {
      if (code == '') {
        wx.showToast({
          title: '验证码不能为空',
          icon: 'none'
        });
        return false;
      }
      if (code.length != 4) {
        wx.showToast({
          title: '验证码长度有误！',
          icon: 'none'
        });
        return false;
      }
      sendData = {
        userType: 1,
        telephone: telephone,
        code: code
      };
    } else {
      if (password == '') {
        wx.showToast({ title: '密码不能为空！', icon: 'none' });
        return false;
      }
      sendData = { userType: 2, telephone: telephone, password: password };
    }
    that.setData({ buthiddenSubmit: false });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'login',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: sendData,
      success: function(res) {
        if (res.data.code == 100) {
          console.log(res.data);
          var userId = res.data.result.id;
          wx.setStorageSync('userId', userId);
          app.globalData.userId = userId;
          that.setData({ buthiddenSubmit: true });
          wx.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1000
          });
          setTimeout(function() {
            wx.reLaunch({
              url: '../home/home'
            });
          }, 1500);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          });
          that.setData({ buthiddenNext: false });
        }
      },
      fail: function(error) {
        that.setData({ buthiddenSubmit: false });
      }
    });
  },
  //去注册
  goRegister: function() {
    wx.navigateTo({
      url: 'registerNext'
    });
  },
  onUnload() {
    //页面卸载  当redirectTo或navigateBack的时候调用
    clearInterval(this.data.timer);
  }
});
